package com.git.smp.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.SmpConsumeMapper;
import com.git.smp.entity.SmpConsume;

@RestController
@RequestMapping("/consume")
public class SmpConsumeController {
	
	Logger logger = LoggerFactory.getLogger(SmpConsumeController.class);
	
	@Autowired
	SmpConsumeMapper smpConsumeMapper;
	
	//根据R值查询对应的P值列表
	@RequestMapping("/getPubCodeList")
	public ResponseMessage<List<SmpConsume>> getPubCodeList(@RequestParam String regCode , @RequestParam String systemNo ){
		ResponseMessage<List<SmpConsume>> responseMessage = new ResponseMessage<List<SmpConsume>>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("根据R值查询对应的P值列表成功！");
		SmpConsume smpConsume = new SmpConsume();
		smpConsume.setBusCode(regCode);
		smpConsume.setSystemNo(systemNo);
		List<SmpConsume> listPubCode = smpConsumeMapper.getConsumeInfo(smpConsume);
		if(listPubCode == null) {
			responseMessage.setCode("0001");
			responseMessage.setMessage("查询P值列表信息失败！");
		}
		responseMessage.setExtend(listPubCode);
		return responseMessage;
	}
	
	//新增调用关系信息
	@RequestMapping("/addConsumeInfo")
	public ResponseMessage<?> addConsumeInfo(
			@RequestBody SmpConsume smpConsume){
		ResponseMessage<?> responseMessage = new ResponseMessage<>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("新增调用关系信息成功！");
		
		int code = smpConsumeMapper.insert(smpConsume);
		
		logger.info(responseMessage.getMessage());
		return responseMessage;
	}
	
	//自动生成消费编码（P值）
	@RequestMapping("/autoGeneratePubCode")
	public ResponseMessage<String> autoGeneratePubCode(){
		ResponseMessage<String> responseMessage = new ResponseMessage<String>();
		responseMessage.setCode("0000");
		responseMessage.setMessage("消费编码P值生成成功！");
		
		int id = smpConsumeMapper.getId();
		String pubCode = "P000" + "10" + String.format("%06d", id);
		
		responseMessage.setExtend(pubCode);
		logger.info(responseMessage.getMessage());
		return responseMessage;
		
	}
	

}
